<?php

namespace app\services;

use app\payment\PayFactory;
use app\traits\Page;
use BaconQrCode\Encoder\QrCode;
use plugin\admin\app\model\Activity;
use plugin\admin\app\model\ActivityCategory;
use plugin\admin\app\model\ActivityImage;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserActivity;
use plugin\admin\app\model\UserCollect;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\UserSurname;
use SimpleSoftwareIO\QrCode\Generator;
use support\Redis;

class ActivityService
{
    public static function list($page, $length, $id = 0, $name = '', $isRecommend = null)
    {
        $offset = ($page - 1) * $length;
        $query = Activity::select([
            'id',
            'img_url',
            'title',
            'desc',
            'created_time',
            'shares',
            'collects'
        ]);

        if (!is_null($isRecommend)) {
            $query = $query->where('is_recommend', $isRecommend);
        }

        if ($id) {
            $query = $query->where('category_id', $id);
        }

        if ($name) {
            $query = $query->where('title', 'like', "%{$name}%");
        }

        $count = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->orderByDesc('created_time')->get()->toArray();

        foreach ($list as $key=>$item) {
            $list[$key]['img_url'] = full_url($item['img_url']);

            $list[$key]['is_collect'] = false;
            if (get_user_id()) {
                $list[$key]['is_collect'] = UserCollectService::isCollect(UserCollect::ACTIVITY, $item['id']);
            }
        }

        return Page::wrap($count, $page, $length, $list);
    }

    public static function categories()
    {
        return ActivityCategory::select(['id', 'name'])->get()->toArray();
    }

    public static function detail($id)
    {
        $res = Activity::select([
            'id',
            'category_id',
            'img_url',
            'title',
            'desc',
            'content',
            'created_time',
            'shares',
            'collects',
        ])->where('id', $id)->first();

        if (!$res) {
            throw new \Exception('不存在的活动');
        }

        $res = $res->toArray();
        $res['img_url'] = full_url($res['img_url']);

        if (getenv('STORE_DRIVER') == 'file') {
            $res['content'] = str_replace('src="', 'src="' . getenv('APP_URL'), $res['content']);
        }

        if (get_user_id()) {
            $res['is_collect'] = UserCollectService::isCollect(UserCollect::ACTIVITY, $res['id']);
        }

        $res['category_info'] = ActivityCategory::where('id', $res['category_id'])->first();

        return $res;
    }

    public static function collect($id)
    {
        UserCollectService::collect(UserCollect::ACTIVITY, $id, function() use ($id) {
            Activity::where('id', $id)->increment('collects');
        });
    }

    public static function uncollect($id)
    {
        UserCollectService::uncollect(UserCollect::ACTIVITY, $id, function() use ($id) {
            Activity::where('id', $id)->decrement('collects');
        });
    }

    public static function share($id)
    {
        Activity::where('id', $id)->increment('shares');
    }
}